*************************************************************
* The following code is part of the replication archive of  * 
* Genovese, Kern & Martin (2016) "Policy Alternation", ISQ  * 
*************************************************************

******************************************
* Close countries:  Results in Table A.3  *
******************************************

* MSTAR model with 3 spatial matrices: geographical distance, IGOs and  trade dyads + subsidy 

clear
pr drop _all
set more off
set matsize 800

***********************
*Likelihood Evaluator  
***********************                                                         

program define splag_ll

args lnf mu rho1 rho2 rho3 sigma
tempvar A rSL1 rSL2 rSL3
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
gen double `rSL3'=`rho3'*SL3
scalar p1 = `rho1'
scalar p2 = `rho2'
scalar p3 = `rho3'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix p3W3 = p3*W3
matrix IpW = I_n - p1W1 - p2W2 - p3W3
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'- `rSL3'-`mu', 0, `sigma'))
end


**********************
*Open Data For Weights
**********************

spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/gled_dist_in2_lag.dta", name(W1) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/trade_in2_lag.dta", name(W2) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/IGO_en_in2_lag.dta", name(W3) standardize 


**************************
*Open Data for Regression
**************************

drop _all

use "/Users/.../gkm_replication_isq/green taxes and subsidies/OECD subsidies lag.dta", clear
gen EUdummy=1
replace EUdummy=0 if country=="Australia" | country=="NZ" | country=="USA" | country=="Canada" | country=="Japan" | country=="Turkey" | country=="Czech" |  country=="Hungary" | country=="Switzerlan" |   country=="Norway" | country=="Iceland" |  country=="Poland"  

global nobs = 233 
*258 if capopen is not in
matrix I_n = I($nobs)
global Y greentax_pc_cons
global X lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc   EUdummy

mkmat $Y, matrix(Y)

matrix SL1 = W1*Y
svmat SL1, n(SL1)
matrix SL2 = W2*Y
svmat SL2, n(SL2)
matrix SL3 = W3*Y
svmat SL3, n(SL3)

************************
*Produce starting values
************************ 

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

ml model lf splag_ll (mu: $Y=lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc EUdummy) (rho1:) (rho2:) (rho3:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
ml init sigma:_cons=`OLSsigma'
*ml trace on
ml max, difficult

eststo D1

drop SL11
drop SL21
drop SL31

********************************************

* MSTAR model with spatially lagged subsidy

clear
pr drop _all
set more off
set matsize 800

***********************
*Likelihood Evaluator  
***********************                                                         

program define splag_ll

args lnf mu rho1 rho2 rho3 rho4 sigma
tempvar A rSL1 rSL2 rSL3 rSL4
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
gen double `rSL3'=`rho3'*SL3
gen double `rSL4'=`rho3'*SL4
scalar p1 = `rho1'
scalar p2 = `rho2'
scalar p3 = `rho3'
scalar p4 = `rho4'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix p3W3 = p3*W3
matrix p4W4 = p4*W4
matrix IpW = I_n - p1W1 - p2W2 - p3W3 -  p4W4
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'- `rSL3' - `rSL4'-`mu', 0, `sigma'))
end


**********************
*Open Data For Weights
**********************

spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/gled_dist_in2_lag.dta", name(W1) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/trade_in2_lag.dta", name(W2) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/IGO_en_in2_lag.dta", name(W3) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/gled_dist_in2_lag_close.dta", name(W4) standardize 
**************************
*Open Data for Regression
**************************

drop _all


use "/Users/.../gkm_replication_isq/green taxes and subsidies/OECD subsidies lag.dta", clear
gen EUdummy=1
replace EUdummy=0 if country=="Australia" | country=="NZ" | country=="USA" | country=="Canada" | country=="Japan" | country=="Turkey" | country=="Czech" |  country=="Hungary" | country=="Switzerlan" |   country=="Norway" | country=="Iceland" |  country=="Poland"  

global nobs = 233 
*258 if capopen is not in
matrix I_n = I($nobs)
global Y greentax_pc_cons
global X lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc  EUdummy
global Z l_subsidy_rgdppc

mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)

matrix SL1 = W1*Y
svmat SL1, n(SL1)
matrix SL2 = W2*Y
svmat SL2, n(SL2)
matrix SL3 = W3*Y
svmat SL3, n(SL3)
matrix SL4 = W4*Z
svmat SL4, n(SL4)

spatgsa  greentax_pc_cons , weights(W1) moran /* positive/negative coeff: nearby regions tend to exhibit similar/dissimilar values of Y*/
spatgsa  l_subsidy_rgdppc , weights(W4) moran /* positive/negative coeff: nearby regions tend to exhibit similar/dissimilar values of Y*/

************************
*Produce starting values
************************ 

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

ml model lf splag_ll (mu: $Y=lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc   EUdummy) (rho1:) (rho2:) (rho3:) (rho4:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
ml init sigma:_cons=`OLSsigma'
*ml trace on
ml max, difficult

eststo D2


drop SL11
drop SL21
drop SL31

********************************************

* MSTAR model with interaction with spatially lagged subsidyXeconomic integration

clear
pr drop _all
set more off
set matsize 800

***********************
*Likelihood Evaluator  
***********************                                                         

program define splag_ll

args lnf mu rho1 rho2 rho3 rho4 sigma
tempvar A rSL1 rSL2 rSL3 rSL4
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
gen double `rSL3'=`rho3'*SL3
gen double `rSL4'=`rho3'*SL4
scalar p1 = `rho1'
scalar p2 = `rho2'
scalar p3 = `rho3'
scalar p4 = `rho4'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix p3W3 = p3*W3
matrix p4W4 = p4*W4
matrix IpW = I_n - p1W1 - p2W2 - p3W3 -  p4W4
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'- `rSL3' - `rSL4'-`mu', 0, `sigma'))
end


**********************
*Open Data For Weights
**********************

spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/gled_dist_in2_lag.dta", name(W1) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/trade_in2_lag.dta", name(W2) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/IGO_en_in2_lag.dta", name(W3) standardize 
spatwmat using "/Users/.../gkm_replication_isq/green taxes and subsidies/spatial weights/gled_dist_in2_lag_close.dta", name(W4) standardize 

**************************
*Open Data for Regression
**************************

drop _all


use "/Users/.../gkm_replication_isq/green taxes and subsidies/OECD subsidies lag.dta", clear 
gen EUdummy=1
replace EUdummy=0 if country=="Australia" | country=="NZ" | country=="USA" | country=="Canada" | country=="Japan" | country=="Turkey" | country=="Czech" |  country=="Hungary" | country=="Switzerlan" |   country=="Norway" | country=="Iceland" |  country=="Poland"  

global nobs = 233 
*258 if capopen is not in
matrix I_n = I($nobs)
global Y greentax_pc_cons
global X lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc  EUdummy
global Z l_subsidy_rgdppc

mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)


matrix SL1 = W1*Y
svmat SL1, n(SL1)
matrix SL2 = W2*Y
svmat SL2, n(SL2)
matrix SL3 = W3*Y
svmat SL3, n(SL3)
matrix SL4 = W4*Z
svmat SL4, n(SL4)

************************
*Produce starting values
************************ 

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

ml model lf splag_ll (mu: $Y=lag_gtax_pc_cons  rgdppc rgdppcsq unemp unempsq i_itax_per_cap actual_economic_flows rile_const_w env_const_w env_const_w_sq green_party en_prod_rgdp l_subsidy_rgdppc   EUdummy c.SL4#c.actual_economic_flows) (rho1:) (rho2:) (rho3:) (rho4:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
ml init sigma:_cons=`OLSsigma'
*ml trace on
ml max, difficult

eststo D3
